package com.cat.DataStructure04;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-servers-that-communicate/
 * @create 2025/11/26 16:18
 * @since JDK17
 */

public class Solution16 {
    public int countServers(int[][] grid) {
        int n = grid.length, m = grid[0].length, ans = 0;
        int[] row = new int[n], col = new int[m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                row[i] += grid[i][j];
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                col[i] += grid[j][i];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1 && (row[i] > 1 || col[j] > 1)) {
                    ans++;
                }
            }
        }

        return ans;
    }
}
